﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>15. ThinkPHP 3.1.2 模板变量详解</title></head><body>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; line-height: 190%">本节课大纲：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">一、变量输出 &nbsp; &nbsp; &nbsp; &nbsp;（重点）</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;1. 标量输出</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;2. 数组输出</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name[1]} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//索引数组</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name[&apos;k2&apos;]} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//关联数组</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name.k1} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//关联数组</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;3. 对象输出</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name:k} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//调用对象$name的成员属性k</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name-&gt;k} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//调用对象$name的成员属性k</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">二、系统变量</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$Think.get.id}</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">三、使用函数</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name|strtoupper} 生成的编译后文件是 &lt;?php echo (strtoupper($name)); ?&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name|date=&apos;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">Y m d H:i:s</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">&apos;, </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">###</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">} &nbsp; &nbsp; &nbsp; &nbsp;//###点位符，表示第二个参数</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//生成的编译后文件是&lt;?php echo date(&apos;Y m d H:i:s&apos;, $name); ?&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">四、默认值</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name|</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 240%">default</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 240%">=&apos;这里是默认值&apos;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 140%">五、运算符</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ - * / % ++ --</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$name++}</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">+++++++++++++++++++++++++++++++++++++Leo++++++++++++++++++++++++++++++++++++++++</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%">后台分配给模板的变量：模板变量</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%">标量，4种：整型、浮点、字符串、布尔</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">第三方类文件（即自己编写的类），放置在: </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">项目/ThinkPHP/Extend/Library/ORG/自建文件夹（my）/test.class.php</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;代码中导入：</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 240%">import</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 240%">(&apos;ORG.my.test&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%">系统常量：从模板中也可以获得</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">系统变量：在模板中直接输出变量，见手册</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">$Think.get.name</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> 等价于模块中的 $_GET[&apos;name&apos;]</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 240%">$Think.const.？？ &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> 获取常量的值</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">模板中使用函数</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 190%">{$name | md5} &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%">//在模板中用md5来处理$name变量</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;生成的编译后文件可以从</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 240%"> Runtime/cache</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%">文件夹里头查看</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">时间戳：</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">time（）函数</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>